package com.qf.spring.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Aspect//生命是一个通知增强类
public class MyAdvice {
    @Pointcut("execution(* com.qf..impl.*Impl.*(..))")
    public void Pointcut(){

    }

    @Before("Pointcut()")
    public void before(JoinPoint joinPoint ){
        System.out.println("事务开启了++++++++++++++++");
    }

    /**
     * After最终的返回，所有的操作都执行完在执行，不受一场影响
     * @param joinPoint
     */
    @After("Pointcut()")
    public void after(JoinPoint joinPoint){
        System.out.println("事务提交了++++++++++++++++++++++++");
    }
    @AfterReturning(value = "Pointcut()" ,returning = "result")
    public void afterreturn(JoinPoint joinPoint,Object result){
        System.out.println("111111111事务提交了++++++++++++++++++++++++"+result);
    }
    @AfterThrowing(value = "Pointcut()",throwing = "e")
    public void afterThrowing(JoinPoint joinPoint,Exception e){
        System.out.println("afterThrowing执行了----->"+e.getMessage());
        System.out.println("回滚事务");
    }
    @Around(value = "Pointcut()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        //环绕通知是我们自己决定什么时候放行
        System.out.println("环绕通知开启事务");
        //获取原始结果
        Object result = joinPoint.proceed();
        System.out.println("环绕通知提交事务");
        return result;
    }
}
